﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataManager
{
    public partial class frmSzineszekRendezok : Form
    {
        Actors m_Actors;
        Directors m_Directors;
        string selected;

        public frmSzineszekRendezok()
        {
            InitializeComponent();
            m_Actors = new Actors();
            m_Directors = new Directors();
        }

        private void frmSzineszek_Load(object sender, EventArgs e)
        {
            FillCbFields();
            SetInputsState(false);
        }

        /// <summary>
        /// Feltolti a comboboxot
        /// </summary>
        private void FillCbFields()
        {
            cbFields.Items.Add("Színészek");
            cbFields.Items.Add("Rendezők");
        }

        /// <summary>
        /// Feltolti a DataGridViewt a szineszek adataival
        /// </summary>
        private void FillDgvActors()
        {
            dgvActors.Rows.Clear();
            List<Actor> actorList = m_Actors.GetActorList("");
            foreach (Actor item in actorList)
            {
                dgvActors.Rows.Add(item.ActorId, item.ActorName, item.Nationality);
            }
        }

        /// <summary>
        /// Feltolti a DataGridViewt a rendezok adataival
        /// </summary>
        private void FillDgvDirectors()
        {
            dgvActors.Rows.Clear();
            List<Director> directorList = m_Directors.GetDirectorList("");
            foreach (Director item in directorList)
            {
                dgvActors.Rows.Add(item.DirectorId, item.DirectorName, item.Nationality);
            }
        }

        /// <summary>
        /// A valasztasnak megfeleloen feltolti a tablazatot
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbFields_SelectedIndexChanged(object sender, EventArgs e)
        {
            selected = cbFields.SelectedItem.ToString();
            if (selected == "Színészek")
            {
                FillDgvActors();
            }
            else
                if (selected == "Rendezők")
                {
                    FillDgvDirectors();
                }
        }

        /// <summary>
        /// Ha duplan klikkelunk egy szineszre akkor megjelenik egy uj ablak benne a filmek amikben a szinesz jatszott vagy a rendezo rendezte
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvActors_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            frmJatszaRendezte myForm = null;
            if (selected == "Színészek")
            {
                myForm = new frmJatszaRendezte(Convert.ToInt32(dgvActors.SelectedRows[0].Cells["Id"].Value), dgvActors.SelectedRows[0].Cells["Nev"].Value.ToString(),0);
            }
            else
                if (selected == "Rendezők")
                {
                    myForm = new frmJatszaRendezte(Convert.ToInt32(dgvActors.SelectedRows[0].Cells["Id"].Value), dgvActors.SelectedRows[0].Cells["Nev"].Value.ToString(),1);
                }
            Hide();
            myForm.ShowDialog();
            Close();
        }

        /// <summary>
        /// Szures
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSzukit_Click(object sender, EventArgs e)
        {
            dgvActors.Rows.Clear();
            if (selected == "Színészek")
            {
                List<Actor> actorList = m_Actors.GetActorList(tbFilter.Text);
                foreach (Actor item in actorList)
                {
                    dgvActors.Rows.Add(item.ActorId, item.ActorName, item.Nationality);
                }
            }
            else
                if (selected == "Rendezők")
                {
                    List<Director> directorList = m_Directors.GetDirectorList(tbFilter.Text);
                    foreach (Director item in directorList)
                    {
                        dgvActors.Rows.Add(item.DirectorId, item.DirectorName, item.Nationality);
                    }
                }
            
        }

        private void SetInputsState(bool state)
        {
            checkNew.Enabled = state;
            tbNev.Enabled = state;
            tbNemzetiseg.Enabled = state;
            btnMent.Enabled = state;
            btnMegse.Enabled = state;
        }

        private void ClearInputValues()
        {
            tbNev.Text = "";
            tbNemzetiseg.Text = "";
        }

        /// <summary>
        /// Elerhetove valnak az uj adat bevitelehez szukseges elemek
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUj_Click(object sender, EventArgs e)
        {
            SetInputsState(true);
            ClearInputValues();
            checkNew.Checked = true;
        }

        /// <summary>
        /// Elerhetove valnak az adatok bevitelehez szukseges elemek
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnModosit_Click(object sender, EventArgs e)
        {
            SetInputsState(true);
            ClearInputValues();
            if (dgvActors.SelectedRows.Count != 0)
            {
                tbNev.Text = dgvActors.SelectedRows[0].Cells["Nev"].Value.ToString();
                tbNemzetiseg.Text = dgvActors.SelectedRows[0].Cells["Nemzetiseg"].Value.ToString();
            }
        }

        /// <summary>
        /// Beszurja vagy modositja 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMent_Click(object sender, EventArgs e)
        {
            if (checkNew.Checked)
            {
                if (selected == "Színészek")
                {
                    string error = m_Actors.InsertActor(tbNev.Text.ToString(), tbNemzetiseg.Text.ToString());
                    FillDgvActors();
                }
                else
                    if (selected == "Rendezők")
                    {
                        string error = m_Directors.InsertDirector(tbNev.Text.ToString(), tbNemzetiseg.Text.ToString());
                        FillDgvDirectors();
                    }
                ClearInputValues();
                SetInputsState(false);
            }
            else
            {
                if (selected == "Színészek")
                {
                    string error = m_Actors.UpdateActor(Convert.ToInt32(dgvActors.SelectedRows[0].Cells["Id"].Value), tbNev.Text.ToString(), tbNemzetiseg.Text.ToString());
                    FillDgvActors();
                }
                else
                    if (selected == "Rendezők")
                    {
                        string error = m_Directors.UpdateDirector(Convert.ToInt32(dgvActors.SelectedRows[0].Cells["Id"].Value), tbNev.Text.ToString(), tbNemzetiseg.Text.ToString());
                        FillDgvDirectors();
                    }
                ClearInputValues();
                SetInputsState(false);
            }
        }

        /// <summary>
        /// Torli a kijelolt sort
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTorol_Click(object sender, EventArgs e)
        {
            if (dgvActors.SelectedRows.Count != 0)
            {
                if (selected == "Színészek")
                {
                    string error = m_Actors.DeleteActor(Convert.ToInt32(dgvActors.SelectedRows[0].Cells["Id"].Value));
                    FillDgvActors();
                }
                else
                    if (selected == "Rendezők")
                    {
                        string error = m_Directors.DeleteDirector(Convert.ToInt32(dgvActors.SelectedRows[0].Cells["Id"].Value));
                        FillDgvDirectors();
                    }
            }
        }

        private void btnMegse_Click(object sender, EventArgs e)
        {
            SetInputsState(false);
            ClearInputValues();
            checkNew.Checked = false;
        }

        private void btnVissza_Click(object sender, EventArgs e)
        {
            frmDVDk myForm = new frmDVDk();
            Hide();
            myForm.ShowDialog();
            Close();
        }

        private void btnKilepes_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
